#!/bin/bash
####################################
# mysql db backup  #MDB
# 备份数据保存7天            同时 上传ftp
####################################

# cygwin  运行方式
cyg_home=/cygdrive/d/cygwin
PATH=$cyg_home/usr/sbin:$cyg_home/bin:$PATH






#备份路径
db_backup_dir='/cygdrive/d/AdventNet/ME/ServiceDesk/mysql/backup/'
db_log_index='/cygdrive/d/AdventNet/ME/ServiceDesk/mysql/log/mysql_bin.index'

#备份选项 
db_database_user='root'
db_database_pass=''
db_database_host='127.0.0.1' #127.0.0.1 不能用
db_database_port='33366'
#db_database_port='3302'
# 如果是空  就是全部
db_database_name='0401  backup_price_010323  backup_sms_mail  backup_ws_queue_mail  backup_wsnew  crm01  cy  data2  datafromzz91  emailtosend  hlmetal  itcast  mogali  note_sys  performance_evaluation  price  price_backup  sms_index  sms_phoneinfo  sms_v1  sms_v2  sms_v2test  temporary_mail  temporary_mail_en  test  worldscrap_china  ws_xjkh  wsauc  wsauc_v2  wsnew  xjkh    ' #数据库库名  aa bb cc ==  aa.bb.ccd.dd


db_database_name=''
#是否分割 备份文件
do_split=0
split_size=50
#数据库 命令
cmd_date=$cyg_home/bin/date.exe 
cmd_mysqldump=/cygdrive/d/AdventNet/ME/ServiceDesk/mysql/bin/mysqldump.exe 
cmd_mysql=/cygdrive/d/AdventNet/ME/ServiceDesk/mysql/bin/mysql.exe 


echo " init config "
#  ---------- init 检查设置
#备份目录
if [ ! -e  $db_backup_dir  ]
then 
    echo " make backup dir "
	mkdir -p  $db_backup_dir 
fi 

if [[ ! -e $db_log_index  ]]
then
    echo " bin_log_index not exists "
    return 21
fi    




echo " back 备份开始";
#-----------------------内部变量
_db_dump_arg=''
_db_mysql_arg=''
_mysql_con=''
_db_dump_file=''
_db_backup_id=$( date +%s)
_db_date_w=$(date +'%w')
_db_split_arg=''
_db_dump_pipe=./backup.pipe


#空格，替换为，
_db_database_file=$( echo  $db_database_name | sed -n  's/[ ]/,/gp ' )
if [ -z $_db_database_file  ]
then 
	_db_database_file='all'
fi
_db_database_file=$(   expr substr $_db_database_file 1 10 ) 
echo  '_db_database_file==' $_db_database_file
#------split 输出
if [[  $do_split -eq 1   ]]
then
    #通道文件
    if [[ ! -e  $_db_dump_pipe   ]]
    then 
        mkfifo $_db_dump_pipe
    fi 
    _db_split_arg=${_db_split_arg}"  -b ${split_size}m   $_db_dump_pipe "
    _db_split_arg=${_db_split_arg}${db_backup_dir}${_db_database_file}_${_db_date_w}_

    echo $_db_split_arg
    split $_db_split_arg      &
    _db_dump_file=$_db_dump_pipe
else
    _db_dump_file=${db_backup_dir}${_db_database_file}_${_db_date_w}.sql
fi
#--------连接
_mysql_con=''
_mysql_con=${_mysql_con}" -u ${db_database_user}    "
if [[ ! -z $db_database_pass ]]
then
    _mysql_con=${_mysql_con}"  -p${db_database_pass}  "
fi
_mysql_con=${_mysql_con}" -h ${db_database_host} -P ${db_database_port} "
#--------dump 
_db_dump_arg=${_mysql_con}
if [[ ! -z  $db_database_name  ]] 
then    
     _db_dump_arg=${_db_dump_arg}" --databases ${db_database_name}  "
 else
     _db_dump_arg=${_db_dump_arg}" --all-databases  "
fi 
_db_dump_arg=${_db_dump_arg}' --default-character-set=utf8   --set-charset    ' 
_db_dump_arg=${_db_dump_arg}' --hex-blob ' 
#_db_dump_arg=${_db_dump_arg}' --delete-master-logs     ' 
#_db_dump_arg=${_db_dump_arg}' --master-data --lock-all-tables ' 

#--------mysql 
_db_mysql_arg=${_mysql_con}

echo $_db_dump_arg
echo $_db_mysql_arg
#-----do      缺点，先清空日志，后备份 （优点 可在线不锁表） （一个数据不全，数据有重复）
#echo ' reset master; ' | mysql $_db_mysql_arg  
$cmd_mysqldump  $_db_dump_arg  > $_db_dump_file  2>>./backup_db.error   
_db_dump_state=$? 
#----tag log
if [[ $_db_dump_state -eq 0 ]] 
then
    cp  $db_log_index   ${db_log_index}_$( date +'%Y_%m_%d_%H_%M_%S' ) 
    echo ' FLUSH LOGS;  ' | $cmd_mysql  $_db_mysql_arg  
    #echo " ----  $( date +'%Y_%m_%d_%H_%M_%S' )----" >>  $db_log_index  
fi

#------------
echo dump_state $_db_dump_state






exit 0


 
#------------upload to  ftp
ftp   <<ENDX
prompt off
open  192.168.0.131




user oracle oracle
cd ftp/ip85
lcd  D:\AdventNet\ME\ServiceDesk\mysql\backup
#----------    
!ls
ls -l 
binary 
mput *.sql 
#----------
quit
ENDX



  
echo dump_state $_db_dump_state
	



